from ui import UI
from ulibs import rectify_main, img_project, extract_main, evaluate_main
from argparse import ArgumentParser
import numpy as np


def args_parser():
    args_parse = ArgumentParser()
    args_parse.add_argument(
        "--mode",
        type=str,
        default="ui",
        help="ui and cmd for rectifying, eva for evalating. Or you can use cmd_eva for both",
    )
    args_parse.add_argument(
        "--show",
        type=str,
        default=True,
        help="determine whether to show the divided lines in the raw image",
    )
    args_parse.add_argument(
        "--postfix",
        type=str,
        default="",
        help="the postfix of the background image, specially for noisy images",
    )
    args = args_parse.parse_args()
    return args


def vector_product(vec_1, vec_2):
    return [
        (vec_1[1] * vec_2[2] - vec_1[2] * vec_2[1]),
        (vec_1[2] * vec_2[0] - vec_1[0] * vec_2[2]),
        (vec_1[0] * vec_2[1] - vec_1[1] * vec_2[0]),
    ]


def pocket(_lines: list[list[list, list]], **kwargs):
    """
    接受前端的输入，并转换为后端可以识别的格式
    """
    input = list(
        [
            *vector_product(line[0][0:3], line[0][3:6]),
            *vector_product(line[1][0:3], line[1][3:6]),
        ]
        for line in _lines
    )
    print(_lines)
    return rectify_main(input, **kwargs)


if __name__ == "__main__":
    args = args_parser()
    P = np.array([[1, 0, 0], [0, 1, 0], [0.0001, 0.0001, 1]])
    A = np.array([[0.8, 0.2, 0], [0, 1 / 0.8, 0], [0, 0, 1]])
    H = (
        A @ P
    )  # Only when there is no rotation can it be rectified using five orthgonally crossing lines.
    line_id = [
        # [10, 1, 4],
        # # [3, 5, 4],
        # [8, 2, 4],
        # [2, 2, 3],
        # [6, 0, 3],
        # [11, 5, 3],
    ]
    line_id_ext = [
        # [1, 0, 2],
        # [2, 1, 2],
        # [1, 1, 3],
        # [3, 0, 1],
        # # [5,0, 3],
        # [4, 5, 3],
        # # [9, 4, 5],
    ]
    is_show = False if args.show == "False" else True
    if args.mode == "ui":
        print("-----user interface mode------")
        ui = UI()
        #  ui = UI("../check_2.png")
        ui.run(pocket, is_show=args.show)
    elif args.mode == "cmd":
        print("-----command mode------")
        # apply projective transform to raw pictures
        img_project(
            H,
            postfix=args.postfix,
            bg_path="./bg_img",
            output_dir="./output",
        )
        rectify_main(
            *extract_main(
                line_id,
                output_dir="./output",
                is_force=False,
                is_show=is_show,
                img_name="img_project" + args.postfix,
            ),
            output_dir="./output",
            img_name="rectified_img" + args.postfix,
        )
    elif args.mode == "eva":
        print("-----evaluate mode------")
        lines_proj, src_img = extract_main(
            line_id,
            output_dir="./output",
            is_force=False,
            is_show=is_show,
            img_name="img_project" + args.postfix,
        )
        evaluate_main(
            *extract_main(
                line_id_ext,
                output_dir="./output",
                is_force=False,
                is_show=True,
                img_name="rectified_img" + args.postfix,
            ),
            lines_proj=lines_proj,
            line_id=line_id_ext,
            H=H,
        )
    elif args.mode == "cmd_eva":
        print("-----command and evaluation mode------")
        # apply projective transform to raw pictures
        img_project(
            H,
            postfix=args.postfix,
            bg_path="./bg_img",
            output_dir="./output",
        )
        lines_proj, src_img = extract_main(
            line_id,
            output_dir="./output",
            is_force=False,
            is_show=is_show,
            img_name="img_project" + args.postfix,
        )
        rectify_main(
            lines_proj,
            src_img,
            output_dir="./output",
            img_name="rectified_img" + args.postfix,
        )
        evaluate_main(
            *extract_main(
                line_id_ext,
                output_dir="./output",
                is_force=False,
                is_show=True,
                img_name="rectified_img" + args.postfix,
            ),
            lines_proj=lines_proj,
            line_id=line_id_ext,
            H=H,
        )
    else:
        raise ValueError(f"Wrong input key in --mode={args.mode}")
